$(function () {
	
    var cApi = new common();
    
    if(cApi.getQueryString('version')>9.4){
      dynamicLoadCss("./start/css/Approval.css");
    }else{
      dynamicLoadCss("./start/css/Approval.1.css");  
    }
  
    var hostUrl = 'http://117.29.162.206:8089/'  //测试服
    // var hostUrl = 'http://121.204.136.234:48000/'  //正式服
    var billId = cApi.getQueryString('billId');
    var billKey = cApi.getQueryString('billKey');
    var jiamikey = cApi.getQueryString('aesKey');
    var appLoginToken = cApi.getQueryString('appLoginToken');
    var username = cApi.getQueryStringActivity('username');
    var userdepartment = cApi.getQueryStringActivity('userdepartment');
    var usertime = strDateFormat(cApi.getQueryString('usertime'), 16);
    var type = cApi.getQueryString('type');   //ios或者安卓  安卓是1 ios是0


    // var hostUrl = 'http://localhost:8089/'
    // var billId="60530010";  //对应的单号id
    // var billKey="Flow_InvestContract";  //单号名称
    // var jiamikey="3DBE9E92C1209755";  //用户重新登入会改变，测试时找后台要
    // var appLoginToken="b3d03a7ee3174b739fb57abe1f0c9364";   //用户重新登入会改变，测试时找后台要
    // var username = "陈小编";
    // var userdepartment = "招商部";
    // var usertime = "2018-9-9 10:40";
    // var type = 1;   //ios或者安卓  安卓是1 ios是0
  
    var jiamiiv = "16-Bytes--String";
  //   var url;
  //   var zuizhongdata;//用来存放处理完成后的流程数据
    var fujian = [];//用来存放附件
  
    var userimg = "./start/img/tou.png";
    $('.head-avater img').attr('src', userimg);
    $('.userdata-name').text(username);
    $('.userdata-department').text(userdepartment);
    $('.userdata-time').text(usertime);
  
  
 
  
     var zhanshi, listdata, celist;
 
  
  
    var key = CryptoJS.enc.Utf8.parse(jiamikey);  //加密解密用的key
    var iv = CryptoJS.enc.Utf8.parse(jiamiiv);  //加密解密用的iv
  //   //$('#yulan').media({width:'100%', height:'100%',autoplay: true,src:'http://192.168.1.48:8089/erp/BillAttach/Flow_ApplyLeave/39820119/瑞石VI.pdf'});  
  //   // $('.media').media();
   
  
  ProcessDetails();
    
  
   function dynamicLoadCss(url) {
          var head = document.getElementsByTagName('head')[0];
          var link = document.createElement('link');
          link.type='text/css';
          link.rel = 'stylesheet';
          link.href = url;
          head.appendChild(link);
      }
    //获取流程数据
    function ProcessDetails() {
      var dataweijiami = "{billId:'" + billId + "',billKey:'" + billKey + "'}"; //接口传递的参数
      var jianmidata = Encrypt(dataweijiami);  //进行aes加密
      var chanshu = "{appLoginToken:'" + appLoginToken + "',data:'" + jianmidata + "'}"; //数据拼接
      url = hostUrl + "Yigo1.6/services/MapWebService?wsdl"; //请求地址
      var fanhuidata = SoapRequest("WorkFlowWebService", "flowbill", chanshu, url); //调用请求
      //$('.userdata-name').text(fanhuidata);
       var jiemifanhuidata = Decrypt(fanhuidata.data);  //请求成功返回的data进行解密
       //$('.userdata-name').text(jiemifanhuidata);
      zuizhongdata = JSON.parse(jiemifanhuidata); //解密后转成json格式
      
      //  附件数据填充
      for (var i = 0; i < zuizhongdata.billfile.length; i++) {
         fujian.push({ herf: zuizhongdata.billfile[i].filePath, fjm: zuizhongdata.billfile[i].fileName });
       }
       status = zuizhongdata.billhead[0].status == 0 ? "未提交" : zuizhongdata.billhead[0].status == 2 ? "审核中" : "已完成";
      
      //状态填充
      $('.status-text').text(status);
      //数据填充
      tianchong(billKey);
  
  
    }
  
  
  
  
    // 请求函数
    function SoapRequest(interface, method, parameter) {
      var data;
      //SOAP 1.1 请求报文格式，1.2在网上可以找到
      data = '<?xml version="1.0" encoding="utf-8"?>';
      data += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://webservice.mid.myerp.bokesoft.com" xmlns:q0="http://schemas.xmlsoap.org/soap/encoding/" xmlns:something="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
      data += '<soapenv:Body>';
      data += '<ns0:unsafeInvokeService>';
      data += '<sServiceName soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:string">'+interface+'</sServiceName>';
      data += '<args q0:arrayType="xsd:anyType[2]" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="q0:Array">';
      data += '<q0:string>'+method+'</q0:string>';
      data += '<q0:string>'+parameter+'</q0:string>';
      data += '</args>';
      data += '</ns0:unsafeInvokeService>';
      data += '</soapenv:Body>';
      data += '</soapenv:Envelope>';
  
      var xmlhttp = CreateXMLHttp();
         
      var URL = url;
      xmlhttp.open("POST", URL, false);
      xmlhttp.setRequestHeader("Content-Type", "application/soap+xml; charset=utf-8"); //SOAP 1.1为text/xml ； 1.2为 application/soap+xml
      xmlhttp.setRequestHeader("SOAPAction", "wsdl");
   
      xmlhttp.send(data);
  
  
  
  
      var dd = xmlhttp.responseXML.getElementsByTagName("unsafeInvokeServiceReturn");
        
        
      var ee = dd[0].textContent;
      
      var ff = JSON.parse(ee);
    
      if(ff.status==-5){
          if (type == 0) {
            window.webkit.messageHandlers.Submission.postMessage('-5');
          } else {
            window.android.showOfflineWarm();
          }
      
      }
      
      return ff;
      
  
    }
    

    //aes加密
    function Encrypt(word) {
      srcs = CryptoJS.enc.Utf8.parse(word);
      var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, Padding: CryptoJS.pad.Pkcs7 });
      return encrypted.ciphertext.toString().toUpperCase();
    }
    //aes解密
    function Decrypt(word) {
      var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
      var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
      var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, Padding: CryptoJS.pad.Pkcs7 });
      var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
      return decryptedStr.toString();
    }
  
    //状态按钮点击事件
    $('.head-status').on('click', function () {
        if (type == 0) {
        window.webkit.messageHandlers.jump.postMessage(billId);
        } else {
        window.android.goApprovalFlow(billId);
        }
    })
  
  
   
  
    //附件点击事件
    var ii = 1;
    $('.Enclosure').on('click', function () {
      var fujianurl = $(this).attr("data-url");
      if (type == 0) {
        window.webkit.messageHandlers.Enclosure.postMessage(fujianurl);
      } else {
        window.android.showAnnex(fujianurl,"");
      }
    })
  
  
  
  
  
    function tianchong(index) {
      var Data;
      var existsAttachment = true;// 用于设置是否显示附件
      var existsDetail = true;// 用于设置是否显示明细
      var detailTitle = "明细信息";// 用于设置明细标题
      $('.reason-bohui-name').text("发起人：" + zuizhongdata.billhead[0].creator);
      switch (index) {
        // case "Flow_ApplyLeave" :
        // var minxi = [];
        //   existsAttachment = false;
        //   existsDetail = false;
        //   Data = {
        //     jibenxinxi: [
        //       {leixing:"单据日期",neirong:'1111111111111'},
        //         {leixing:"申请人",neirong:'111111111111122'},
        //         {leixing:"部门",neirong:'111111111114444411'},
        //         {leixing:"类型",neirong:'111111111113431'},
        //         {leixing:"休假时长",neirong:'111111111113431'},
        //         {leixing:"休假时间",neirong:'111111111113431'},
        //         {leixing:"休假事由",neirong:'电饭锅电饭锅电饭锅电饭锅的'},
        //         {leixing:"附件",neirong:''}
        //     ],
        //     fujian: fujian,
        //     mingxi: {
        //       toubu: [],
        //       data: minxi,
        //     }
        //   }; break;
        // case "Flow_ApplyLeave" :Data=[
        //   {leixing:"单据日期",neirong:'1111111111111'},
        //   {leixing:"申请人",neirong:'111111111111122'},
        //   {leixing:"部门",neirong:'111111111114444411'},
        //   {leixing:"类型",neirong:'111111111113431'},
        //   {leixing:"休假时长",neirong:'111111111113431'},
        //   {leixing:"休假时间",neirong:'111111111113431'},
        //   {leixing:"休假事由",neirong:'电饭锅电饭锅电饭锅电饭锅的'},
        //   {leixing:"附件",neirong:''}
        // ];break;
      
        case "Flow_InvestContract":
          var minxi = [];
          for (var i = 0; i < zuizhongdata.billdtl.length; i++) {
            minxi.push(["",zuizhongdata.billdtl[i].rowNo,
                        zuizhongdata.billdtl[i].feeName, 
                        zuizhongdata.billdtl[i].houseName,
                        zuizhongdata.billdtl[i].locationName,
                        zuizhongdata.billdtl[i].unit,
                        zuizhongdata.billdtl[i].qty,
                        zuizhongdata.billdtl[i].price,
                        zuizhongdata.billdtl[i].money,
                      ])
          }
          Data = {
            jibenxinxi: [
              { leixing: "标题", neirong: zuizhongdata.billhead[0].billTitle },
              { leixing: "经办部门", neirong: zuizhongdata.billhead[0].deptName },
              { leixing: "经办人", neirong: zuizhongdata.billhead[0].empName },
              { leixing: "合同类别", neirong: zuizhongdata.billhead[0].contractType },
              { leixing: "客户名称", neirong: zuizhongdata.billhead[0].deaName },
              { leixing: "合同金额", neirong: zuizhongdata.billhead[0].contractMoney },
              { leixing: "总期数", neirong: zuizhongdata.billhead[0].totalPeriods },
              { leixing: "当前期数", neirong: zuizhongdata.billhead[0].currentPeriods },
              { leixing: "合同起", neirong: strDateFormat(zuizhongdata.billhead[0].dateFrom, 10) },
              { leixing: "合同止", neirong: strDateFormat(zuizhongdata.billhead[0].dateTo, 10) },
              { leixing: "签订起", neirong: strDateFormat(zuizhongdata.billhead[0].signDateFrom, 10) },
              { leixing: "签订止", neirong: strDateFormat(zuizhongdata.billhead[0].signDateTo, 10) },
              { leixing: "备注", neirong: zuizhongdata.billhead[0].purpose }
            ],
            fujian: fujian,
  
            mingxi: {
              toubu: ["序号","费用名称","房源名称","储位名称","计量单位","数量","单价","价格"],
              data: minxi,
              showPage: 2
            }
          }; break;
     
      }
  
      listdata = {
        data: Data
      }
      
      listdata.data.existsAttachment = existsAttachment;  // 用于设置是否显示附件
      listdata.data.existsDetail = existsDetail ; // 用于设置是否显示明细
      listdata.data.detailTitle = detailTitle ; // 用于设置明细标题
      var zhanshi = document.getElementById('zhanshi').innerHTML;
  
      celist = juicer(zhanshi, listdata);
      $('.approval-data').empty().append(celist);
  
    }
  })